import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import 'logic.dart';

class NoticeReportPage extends StatelessWidget {
  NoticeReportPage({Key? key}) : super(key: key);

  final logic = Get.find<NoticeReportLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(logic.actionTitleMap[logic.reportAction.value] ?? ''),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: Form(
        key: logic.formKey,
        child: Obx(() {
          return Container(
            padding: EdgeInsets.all(kSpaceMedium),
            child: Column(
              children: [
                logic.reportAction.value == NoticeReportLogic.actionProgress
                    ? _buildProgressInput()
                    : _buildReturnInput(),
                SizedBox(height: kSpaceMedium),
                Text(
                  logic.reportAction.value == NoticeReportLogic.actionProgress
                      ? '温馨提示：有效的进度描述更有利于多方的协调沟通以及监督。'
                      : '温馨提示：退回一个案子，间隔12个小时后会自动增加1个配额。',
                  style: TextStyle(
                    fontSize: kFontSizeTiny,
                    color: AppColor.colorFontGrey,
                  ),
                ),
                SizedBox(height: kSpaceMedium),
                _buildSubmitButton(),
              ],
            ),
          );
        }),
      ),
    );
  }

  Widget _buildProgressInput() {
    return TextFormField(
      controller: logic.progressController,
      maxLines: 5,
      decoration: InputDecoration(
        hintText: '请输入当前案源进展描述',
        hintStyle: TextStyle(
          fontSize: kFontSizeSmall,
          color: AppColor.colorFontLabelGrey,
        ),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        contentPadding: EdgeInsets.symmetric(
            vertical: kSpaceSmall, horizontal: kSpaceMedium),
      ),
      validator: (value) {
        if (value == null || value.isEmpty) {
          return '请输入当前案源进展描述';
        }
        return null;
      },
    );
  }

  Widget _buildReturnInput() {
    return Column(
      children: [
        DropdownButtonFormField2<String>(
          value: logic.reportContent.value,
          iconStyleData: const IconStyleData(
              icon: Icon(
            Icons.keyboard_arrow_down_rounded,
            color: AppColor.colorFontBlack,
          )),
          decoration: InputDecoration(
            hintText: '请选择退回原因',
            hintStyle: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontGrey,
            ),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(kRadiusMedium),
            ),
            contentPadding: EdgeInsets.symmetric(
                vertical: kSpaceSmall, horizontal: kSpaceMedium),
          ),
          dropdownStyleData: const DropdownStyleData(
            padding: EdgeInsets.all(0),
            elevation: 0,
            decoration: BoxDecoration(
              border: Border.fromBorderSide(
                BorderSide(color: Color(0xFFE3E6EB)),
              ),
              borderRadius: BorderRadius.all(Radius.circular(4)),
              color: Colors.white,
            ),
          ),
          alignment: Alignment.centerLeft,
          style: TextStyle(
            color: AppColor.colorFontBlack,
            fontSize: kFontSizeSmall,
            fontWeight: AppFont.fontRegular,
          ),
          items: logic.returnContents
              .map((e) => DropdownMenuItem<String>(
                    value: e,
                    child: Text(e),
                  ))
              .toList(),
          onChanged: (value) {
            logic.onSelectContent(value ?? '');
          },
          validator: (value) {
            if (value == null || value.isEmpty) {
              return '请选择退回原因';
            }
            return null;
          },
        ),
        SizedBox(height: kSpaceMedium),
        Obx(() {
          if (logic.reportContent.value == logic.returnContents.last) {
            return TextFormField(
              controller: logic.returnController,
              maxLines: 5,
              decoration: InputDecoration(
                hintText: '请输入退回原因',
                hintStyle: TextStyle(fontSize: kFontSizeSmall),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(kRadiusSmall),
                ),
                contentPadding: EdgeInsets.symmetric(
                    vertical: kSpaceSmall, horizontal: kSpaceMedium),
              ),
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入退回原因';
                }
                return null;
              },
            );
          }
          return Container();
        }),
      ],
    );
  }

  Widget _buildSubmitButton() {
    return Container(
      width: double.infinity,
      height: 50.w,
      margin: EdgeInsets.symmetric(horizontal: kSpaceMedium),
      child: FilledButton(
        onPressed: () => logic.onSubmit(),
        child: const Text('提交'),
      ),
    );
  }
}
